package com.formiaomiao.demain.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ForgetActivity extends BaseActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.et_forget_user)
    EditText etForgetUser;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.et_forget_pwd)
    EditText etForgetPwd;
    @BindView(R.id.et_forget_pwd2)
    EditText etForgetPwd2;
    @BindView(R.id.et_forget_verification)
    EditText etForgetVerification;
    @BindView(R.id.tv_forget_verification)
    TextView tvForgetVerification;

    LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        ButterKnife.bind(this);
        ViewInit();
        ViewInit();
    }

    public void ViewInit() {
        tvClassName.setText("忘记密码");
        loadingDialog = new LoadingDialog(this);
    }

    protected void onStop() {
        super.onStop();
    }

    /**
     * 发送验证码
     */
    public void SendSMS(String username) {
        loadingDialog.show();
        HttpParams params = new HttpParams();
        params.put("phone", username);
        OkGo.<String>post(BaseUrl.USERSENDSMSMESSAGE).params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                loadingDialog.dismiss();
                if (response.body().contains("200")) {
                    ToastUtils.show(ForgetActivity.this, "验证码发送成功，请注意查收");
                } else {
                    ToastUtils.show(ForgetActivity.this, "验证码发送失败");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingDialog.dismiss();
            }
        });
    }

    private void ForGetPwd(String username, String pwd, String code) {
        loadingDialog.show();
        HttpParams params = new HttpParams();
        params.put("userName",username);
        params.put("passWord",pwd);
        params.put("validateCode",code);
        OkGo.<String>post(BaseUrl.USERFORGETPWD).params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                loadingDialog.dismiss();
                if(response.body().contains("200")){
                    ToastUtils.show(ForgetActivity.this,"密码修改成功，请登录");
                    finish();
                }else{
                    ToastUtils.show(ForgetActivity.this,"密码修改失败");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingDialog.dismiss();
            }
        });
    }

    @OnClick({R.id.iv_class_back, R.id.btn_login, R.id.tv_forget_verification})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.btn_login:
                if (etForgetUser.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                } else if (etForgetPwd.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请输入新密码", Toast.LENGTH_SHORT).show();
                } else if (etForgetPwd2.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请再输入新密码", Toast.LENGTH_SHORT).show();
                } else if (etForgetVerification.getText().toString().isEmpty()) {
                    Toast.makeText(this, "请再输入验证码", Toast.LENGTH_SHORT).show();
                } else if (!etForgetPwd.getText().toString().equals(etForgetPwd2.getText().toString().trim())) {
                    Toast.makeText(this, "密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                } else {
                    ForGetPwd(etForgetUser.getText().toString().trim(),etForgetPwd.getText().toString().trim(),
                            etForgetVerification.getText().toString().trim());
                }
                break;
            case R.id.tv_forget_verification:
                if (etForgetUser.getText().toString().isEmpty()) {
                    ToastUtils.show(this, "请输入手机号");
                } else {
                    SendSMS(etForgetVerification.getText().toString());
                }
                break;
        }
    }

}
